﻿using System;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections.Generic;
using static Titan.FIS.SampleRecd.TagGen;
using System.Drawing.Drawing2D;
using Titan.FIS.Barcode.TwoBarcode.Util;
using System.Drawing.Imaging;

namespace Titan.FIS.SampleRecd
{
	/// <summary>
	/// 打印标签类
	/// </summary>
	internal class TagPrint
	{
		private Image _imgContext = null;
		private int _iPrintBarcodeType = 1;

		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="imgContext"></param>
		internal TagPrint(Image imgContext, int iPrintBarcodeType)
		{
			this._imgContext = imgContext;
			this._iPrintBarcodeType = iPrintBarcodeType;
		}

		/// <summary>
		/// 在符合约定的条码打印机的指定尺寸的纸张上打印
		/// </summary>
		/// <returns></returns>
		internal bool Print()
		{
			bool bFlag = false;
			PrintDocument prtDoc = null;
			try
			{
				prtDoc = GetPrintDocument();
				if (prtDoc != null)
				{
					//检查并获得符合约定的条码打印机和纸张
					string tagPrinterName = "";
					PaperSize tagPaperSize = null;
					if (CheckPrinterAndPaper(prtDoc, out tagPrinterName, out tagPaperSize))
					{
						//指定打印机名称
						prtDoc.PrinterSettings.PrinterName = tagPrinterName;
						//PaperSize ps = new PaperSize("Custom Size 1", 40, 30);
						//prtDoc.DefaultPageSettings.PaperSize = tagPaperSize;
						//指定打印机所用的纸张
						//prtDoc.DefaultPageSettings.PaperSize = new PaperSize("test", 207, 236);
						//开始打印
						prtDoc.Print();
						bFlag = true;
					}
				}
			}
			catch (Exception ex)
			{
				throw new Exception("Print" + ex.ToString());
			}
			finally
			{
				if (prtDoc != null)
				{
					prtDoc.Dispose();
				}
			}
			return bFlag;
		}

		/// <summary>
		/// 打印前选择打印机、打印份数和其他打印选项，再打印。
		/// </summary>
		/// <returns></returns>
		private void PrintByUserSelect()
		{
			PrintDocument prtDoc = null;
			try
			{
				prtDoc = GetPrintDocument();
				if (prtDoc != null)
				{
					PrintDialog dlgPrt = new PrintDialog();
					dlgPrt.Document = prtDoc;
					if (dlgPrt.ShowDialog() == DialogResult.OK)
					{
						prtDoc.Print();
					}
				}
			}
			catch
			{
			}
			finally
			{
				if (prtDoc != null)
				{
					prtDoc.Dispose();
				}
			}
		}

		/// <summary>
		/// 在符合约定的条码打印机的指定尺寸的纸张上打印预览
		/// </summary>
		internal bool Preview()
		{
			bool _bReturn = false;
			PrintDocument prtDoc = null;
			try
			{
				prtDoc = GetPrintDocument();
				if (prtDoc != null)
				{
					///检查并获得符合约定的条码打印机，并返回指定尺寸的纸张
					string tagPrinterName = "";
					PaperSize tagPaperSize = null;
					if (CheckPrinterAndPaper(prtDoc, out tagPrinterName, out tagPaperSize))
					{
						//指定打印机名称
						prtDoc.PrinterSettings.PrinterName = tagPrinterName;
						//指定打印机所用的纸张
						prtDoc.DefaultPageSettings.PaperSize = tagPaperSize;
						//显示预览窗体
						PrintPreviewDialog dlgPrev = new PrintPreviewDialog();
						dlgPrev.Document = prtDoc;
						dlgPrev.ShowDialog();
						_bReturn = true;
					}
				}
			}
			catch
			{
			}
			finally
			{
				if (prtDoc != null)
				{
					prtDoc.Dispose();
				}
			}
			return _bReturn;
		}

		/// <summary>
		/// /检查并获得系统中已安装的打印机中是否有符合约定的条码打印机
		/// 约定规则：1、打印机列表中含有 zebra 105sl 字符；
		///           2、纸张列表中含有100*76mm的纸张
		/// </summary>
		/// <param name="prtDoc"></param>
		/// <param name="tagPrinterName"></param>
		/// <param name="tagPaperSize"></param>
		/// <returns></returns>
		private bool CheckPrinterAndPaper(PrintDocument prtDoc, out string tagPrinterName, out PaperSize tagPaperSize)
		{
			bool retVal = false;
			tagPrinterName = "";
			tagPaperSize = null;
			foreach (string printerName in PrinterSettings.InstalledPrinters)
			{
				if (printerName.ToLower().IndexOf("honeywell") >= 0)
				{
					prtDoc.PrinterSettings.PrinterName = printerName;//!!!必须设定

					foreach (PaperSize ps in prtDoc.PrinterSettings.PaperSizes)
					{
						tagPrinterName = printerName;//打印机名称
													 //tagPaperSize = ps;//打印纸张
						retVal = true;
						break;
					}
				}
			}

			return retVal;
		}

		/// <summary>
		/// 生成打印文档
		/// </summary>
		/// <returns></returns>
		private PrintDocument GetPrintDocument()
		{
			PrintDocument prtDoc = null;
			if (this._imgContext != null)
			{
				try
				{
					prtDoc = new PrintDocument();
					prtDoc.DocumentName = "标签打印";
					prtDoc.PrintPage += new PrintPageEventHandler(this.OnPrintPage);
				}
				catch (Exception ex)
				{
					if (prtDoc != null)
					{
						prtDoc.Dispose();
						prtDoc = null;
					}
					throw new Exception("GetPrintDocument" + ex.ToString());
				}
			}
			return prtDoc;
		}

		/// <summary>
		/// 打印页面触发事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="ppea"></param>
		private void OnPrintPage(object sender, PrintPageEventArgs e)
		{
			//指定打印位置和区域
			int px = 96, py = 96;
			GetDPI(out px, out py);
			//获取指定尺寸的纸张和条码所对应的像素数
			var width = 50 * px * 10 / 254;
			var height = 60 * py * 10 / 254;
			var printImg = this._imgContext;
			if (this._iPrintBarcodeType == 2)
				printImg = Barcode.TwoDimensionBarcode.KiCutImage(this._imgContext, width, height, 100);
			e.Graphics.DrawImage(printImg, 0, 0, width, height);
		}


	}
}
